
using System;
using System.Linq;
using System.Collections.Generic;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Data.Linq;
namespace Core
{
    [TableName("tblCanBo_KiLuat")]
    public class CanBoKyLuat
    {
        [Identity, PrimaryKey(1)]
        [MapField("MaCBKL")]
        public int ID { get; set; }
        public int MaCanBo { get; set; }
        public int MaKiLuat { get; set; }
        public DateTime? NgayKiLuat { get; set; }
        public string MoTa { get; set; }

        // FK_tblCB_KL_tblcanbo
        [Association(ThisKey = "MaCanBo", OtherKey = "ID", CanBeNull = false)]
        public CanBo CanBo { get; set; }
        // FK_tblCB_KL_tblkyluat
        [Association(ThisKey = "MaKiLuat", OtherKey = "ID", CanBeNull = false)]
        public KyLuat KyLuat { get; set; }

        public static IQueryable<CanBoKyLuat> CanBoKyLuats
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<CanBoKyLuat>();
                }
            }
        }
        public static List<CanBoKyLuat> All()
        {
            return CanBoKyLuats.Select(x => new CanBoKyLuat
            {
                ID = x.ID,
                MaCanBo = x.MaCanBo,
                MaKiLuat = x.MaKiLuat,
                MoTa = x.MoTa,
                NgayKiLuat = x.NgayKiLuat,
                CanBo = x.CanBo,
                KyLuat = x.KyLuat
            }).ToList();
        }
        public static List<CanBoKyLuat> DanhSachKyLuat1CanBo(int maCanBo)
        {
            return All().Where(c => c.MaCanBo == maCanBo).ToList();
        }
        public static CanBoKyLuat Single(int id)
        {
            return CanBoKyLuats.SingleOrDefault(x => x.ID == id);
        }
        public static int Add(CanBoKyLuat entity)
        {
            using (var db = new DataContext())
            {
                object rs = db.InsertWithIdentity(entity);
                return int.Parse(rs.ToString());
            }
        }
        public static int Update(CanBoKyLuat entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int id)
        {
            return CanBoKyLuats.Delete(x => x.ID == id);
        }
        public static int Delete(int maCanBo, int maKyLuat)
        {
            return CanBoKyLuats.Delete(x => x.MaCanBo == maCanBo && x.MaKiLuat == maKyLuat);
        }  
    }
}